package com.ciwong.mobilelib.widget;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.NinePatch;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieDrawable;
import com.ciwong.mobilelib.R;

import java.io.File;
import java.io.InputStream;


public class DownloadProgressBar extends ProgressDialog {
    private Context             mContext;
    private View                contentView;
    private TextView            textView;
    private LottieAnimationView imageView;
    private ProgressBar         mProgressBar;

    public Button getCancelButton() {
        return mCancelButton;
    }
    public boolean isCanCelButtonVisible() {
      if(mCancelButton!=null&&mCancelButton.getVisibility()==View.VISIBLE){
          return true;
      }else {
          return false;
      }
    }

    private Button mCancelButton;


    public DownloadProgressBar(Context context) {
        super(context,R.style.my_progress_dialog);
        this.mContext = context;
        init();
        this.setCancelable(isCanCelButtonVisible());
    }

    @SuppressLint("WrongViewCast")
    private void init() {
        this.contentView =  LayoutInflater.from(mContext).inflate(R.layout.layout_dowload_progressbar, null);
        textView = contentView.findViewById(R.id.tv_msg);
        imageView = contentView.findViewById(R.id.animation_view);
        mProgressBar = contentView.findViewById(R.id.progressBar);
        mCancelButton = contentView.findViewById(R.id.btn_cancel);
        mCancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                hideProgressBar();
                if(mOnDownLoadDialogDismissListener!=null){
                    mOnDownLoadDialogDismissListener.ondDismiss(DownloadProgressBar.this);
                }
            }
        });

        getWindow().setDimAmount(0.8f);
    }


    private static final String TAG = "NewCWProgressBar";
    public void setMessage(CharSequence message) {
        this.textView.setText(message);
        this.textView.setVisibility(View.VISIBLE);
    }

    public void setMessage(int resId) {
        this.setMessage(this.mContext.getString(resId));
    }
    @Override
    public void show() {
        super.show();
        imageView.playAnimation();
        imageView.setRepeatCount(LottieDrawable.INFINITE);
        this.setContentView(this.contentView);
    }






    public  Drawable getDrawableFromJar(String path, Class<?> clazz) {
        return getDrawableFromJar(path, clazz, new Rect(20, 20, 20, 20));
    }

    public  Drawable getDrawableFromJar(String path, Class<?> clazz, Rect padding) {
        File file = new File(path);
        String absolutePath = file.getAbsolutePath();
        InputStream inputstream = clazz.getResourceAsStream(path);

        if(inputstream != null) {
            Bitmap localBitmap = BitmapFactory.decodeStream(inputstream);
            return getNinePathDrawable(localBitmap, padding);
        } else {
            return null;
        }
    }

    public static Drawable getNinePathDrawable(Bitmap localBitmap, Rect padding) {
        if(localBitmap != null) {
            byte[] chunk = localBitmap.getNinePatchChunk();
            boolean result = NinePatch.isNinePatchChunk(chunk);
            if(result) {
                NinePatchDrawable patchy = new NinePatchDrawable(localBitmap, chunk, padding, (String)null);
                patchy.setCallback((Drawable.Callback)null);
                return patchy;
            }
        }

        return new BitmapDrawable(localBitmap);
    }


    public void updateDownloadProgress(int progress) {
        if(mProgressBar.getVisibility()!=View.VISIBLE){
            mProgressBar.setVisibility(View.VISIBLE);
            mProgressBar.setProgress(0);
        }
		if(progress>=0&&progress<=100){
			mProgressBar.setProgress(progress);
		}
    }

    public void hideProgressBar() {
        if(mProgressBar.getVisibility()==View.VISIBLE){
            mProgressBar.setProgress(0);
            mProgressBar.clearAnimation();
            mProgressBar.setVisibility(View.GONE);
        }
	    Log.e(TAG, "hideProgressBar mProgressBar.getVisibility(): "+(mProgressBar.getVisibility()==View.VISIBLE )+"  progress="+mProgressBar.getProgress());
    }

    public void setOnDownLoadDialogDismissListener(OnDownLoadDialogDismissListener onDownLoadDialogDismissListener) {
        mOnDownLoadDialogDismissListener = onDownLoadDialogDismissListener;
    }

    private OnDownLoadDialogDismissListener mOnDownLoadDialogDismissListener;
   public interface OnDownLoadDialogDismissListener{
        void ondDismiss(DownloadProgressBar downloadProgressBar);
    }
}
